package com.example.demo.Controller;

import com.example.demo.service.payService.PayService;
import lombok.AllArgsConstructor;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 *  支付宝支付，控制器
 *
 * @author jjj
 * */
@RestController
@RequestMapping(value = "/pay")
@AllArgsConstructor
public class PayController {

    private PayService payService;

    /**
     *  下单支付
     * */
    @GetMapping(value = "/confirm")
    public Object pay (HttpSession session) throws Exception {
        //这个接口其实应该是post方式的，但是我这里图方便，直接以get方式访问，
        //且返回格式是text/html，这样前端页面就能直接显示支付宝返回的html片段
        //真实场景下由post方式请求，返回code、msg、data那种格式的标准结构，让前端拿到data里的
        //html片段之后自行加载
        String orderId = session.getAttribute("orderId").toString();
        String cartTotalPrice = session.getAttribute("CartTotalPrice").toString();

        return payService.pay(orderId,cartTotalPrice);

    }
    /**
     *  支付成功的回调
     * */
    @PostMapping(value = "/fallback")
    public Object fallback (HttpServletRequest request, HttpSession session, Model model) {
        Map map = request.getParameterMap();
        System.out.println("进入了回调"+map.toString());
        return "ok";
    }

}
